package com.algo.pmtong;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.Configuration;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;

public class  PathFilesReader{
	private FileSystem hdfs = FileSystem.get(new Configuration());
	private List<Path> files;
	private BufferedReader bf;
	private int i;

	public PathFilesReader(Path p) throws IOException{
		FileStatus[] status = hdfs.listStatus(p);
		files = new ArrayList(status.length);
		for(FileStatus st : status)
			if(st.isFile())
				files.add(st.getPath());
		if(files.size() > i)
			bf = new BufferedReader(new InputStreamReader(hdfs.open(files.get(i))));	
	}
	public String readLine()throws IOException{
		String line;
		if((line = bf.readLine()) != null)
			return line;
		else	bf.close();
		i++;
		if(files.size() > i){
			bf = new BufferedReader(new InputStreamReader(hdfs.open(files.get(i))));
			if((line = bf.readLine()) != null)
                        	return line;
		}
		return null;
	}
}
			
		
